import struct
import numpy as np
import cv2
import sys

def deserialize_float_array(filename):
    with open(filename, "rb") as file:
        data = file.read()
        float_size = struct.calcsize("f")
        num_floats = len(data) // float_size
        float_array = struct.unpack("f" * num_floats, data)
        return np.array(float_array, dtype='float64')

def normalize_array(arr):
    min_val = np.min(arr)
    max_val = np.max(arr)
    normalized_arr = (arr - min_val) * (255 / (max_val - min_val))
    return normalized_arr

stride = 32
ancs = 640 // stride
print(ancs)

float_array= deserialize_float_array("build/data.bin")
nums = int(len(float_array)/18)
float_array = np.reshape(float_array,(nums,18))
print(float_array)
img = cv2.imread("533.jpg")
img = cv2.resize(img, (640,640))

'''
for obj in float_array:
    
    #x_c = (obj[-2]+obj[-4])/2
    #y_c = (obj[-1]+obj[-3])/2
    #cv2.circle(img, (int(x_c), int(y_c)), 2,(0,255,0),-1)
    #cv2.circle(img, (int(obj[-4]), int(obj[-3])), 2,(0,0,255),-1)
    #cv2.circle(img, (int(obj[-2]), int(obj[-1])), 2,(0,255,0),-1)
    cv2.rectangle(img, (int(obj[-4]), int(obj[-3])), (int(obj[-2]), int(obj[-1])),(0,255,0),2)
    
    for i in range(5):
        x = int(obj[i*2])
        y = int(obj[i*2+1])
        cv2.circle(img, (x,y), 2,(0,255,0),-1)
    
    x0 = int(obj[0])
    y0 = int(obj[1])
    x1 = int(obj[2])
    y1 = int(obj[3])
    x2 = int(obj[4])
    y2 = int(obj[5])
    x3 = int(obj[6])
    y3 = int(obj[7])
    cv2.line(img,(x0,y0),(x1,y1),(0,255,0),3)
    
cv2.imwrite("test.jpg", img)
'''
idx = []
picked = np.zeros(20)
for i in range(nums):
    idx.append(i)

for i in range(nums-1):
    #print(float_array[i][12])
    #print(float_array[i][10:14])
    for j in range(nums-i-1):
        if float_array[idx[j]][12] < float_array[idx[j+1]][12]:
            temp = idx[j]
            idx[j] = idx[j+1]
            idx[j+1] = temp
print(idx)
picked_size = 0
#print(float_array[0])
for i in range(nums):
    idxi = idx[i]
    keep = 1
    for j in range(len(picked)):
        idxj = int(picked[j])
        #print(float_array[idxi][14])
        x0_a = float_array[idxi][14]
        y0_a = float_array[idxi][15]
        x1_a = float_array[idxi][16]
        y1_a = float_array[idxi][17]
        x0_b = float_array[idxj][14]
        y0_b = float_array[idxj][15]
        x1_b = float_array[idxj][16]
        y1_b = float_array[idxj][17]
        intersectionArea = max(0, min(x1_a, x1_b) - max(x0_a, x0_b)) * max(0, min(y1_a, y1_b) - max(y0_a, y0_b))
        area_a = (x1_a - x0_a) * (y1_a - y0_a)
        area_b = (x1_b - x0_b) * (y1_b - y0_b)
        unionArea = area_a + area_b - intersectionArea
        iou = intersectionArea / unionArea
        print(iou)
        if iou > 0.6:
            keep = 0
            break
    if keep:
        picked[picked_size] = idxi
        picked_size += 1
print(picked[1])
sys.exit()
'''
end = 3*ancs*ancs*29
float_array = float_array[:end]
lasti = 0
k = 0
for i in range(len(float_array)):
    if float_array[i] == 100:
        k += 1
        if i-lasti != 29:
            print(i-lasti) 
        lasti = i

print(k)
'''
#sys.exit()

#import ipdb; ipdb.set_trace()
out = normalize_array(float_array)
for i in range(29):
	reshaped_arr = np.array(out.reshape((3, ancs, ancs, 29)), dtype='uint8')[:, :, :, i].transpose(1, 2, 0)
	reshaped_arr = cv2.resize(reshaped_arr, (640, 640))
	#print(reshaped_arr.shape)

	cv2.imwrite("build/"+str(i)+".jpg", reshaped_arr)
	#cv2.waitKey(1000)
